<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="小程序基础id" prop="baseId">
              <el-input v-model="queryParams.baseId" placeholder="请输入小程序基础id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="主体资质其他证明材料" prop="qualificationOther">
              <el-input v-model="queryParams.qualificationOther" placeholder="请输入主体资质其他证明材料" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="小程序账号名称" prop="accountName">
              <el-input v-model="queryParams.accountName" placeholder="请输入小程序账号名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="名称命中关键词-补充材料" prop="accountSupplemental">
              <el-input v-model="queryParams.accountSupplemental" placeholder="请输入名称命中关键词-补充材料" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="认证类型为个人类型时可以选择要认证的身份" prop="authIdentification">
              <el-input v-model="queryParams.authIdentification" placeholder="请输入认证类型为个人类型时可以选择要认证的身份" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="身份证明材料" prop="authIdentMaterial">
              <el-input v-model="queryParams.authIdentMaterial" placeholder="请输入身份证明材料" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="第三方联系电话" prop="thirdPartyPhone">
              <el-input v-model="queryParams.thirdPartyPhone" placeholder="请输入第三方联系电话" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="appid" prop="serviceAppid">
              <el-input v-model="queryParams.serviceAppid" placeholder="请输入appid" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="认证联系人姓名" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入认证联系人姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="认证联系人邮箱" prop="email">
              <el-input v-model="queryParams.email" placeholder="请输入认证联系人邮箱" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="认证联系人手机号" prop="mobile">
              <el-input v-model="queryParams.mobile" placeholder="请输入认证联系人手机号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="纳税识别号" prop="taxpayerId">
              <el-input v-model="queryParams.taxpayerId" placeholder="请输入纳税识别号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="企业电话" prop="enterprisePhone">
              <el-input v-model="queryParams.enterprisePhone" placeholder="请输入企业电话" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="企业注册地址" prop="enterpriseAddress">
              <el-input v-model="queryParams.enterpriseAddress" placeholder="请输入企业注册地址" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="企业开户银行" prop="bankName">
              <el-input v-model="queryParams.bankName" placeholder="请输入企业开户银行" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="企业银行账号" prop="bankAccount">
              <el-input v-model="queryParams.bankAccount" placeholder="请输入企业银行账号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="发票备注" prop="desc">
              <el-input v-model="queryParams.desc" placeholder="请输入发票备注" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['platform:miniProgramAuthentication:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['platform:miniProgramAuthentication:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['platform:miniProgramAuthentication:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['platform:miniProgramAuthentication:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="miniProgramAuthenticationList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" v-if="true" />
        <el-table-column label="小程序基础id" align="center" prop="baseId" />
        <el-table-column label="认证主体类型：1.企业；12.个体工商户；15.个人" align="center" prop="customerType" />
        <el-table-column label="支付方式 1：消耗服务商预购包 2：小程序开发者自行支付" align="center" prop="payType" />
        <el-table-column label="主体资质其他证明材料" align="center" prop="qualificationOther" />
        <el-table-column label="小程序账号名称" align="center" prop="accountName" />
        <el-table-column label="小程序账号名称命名类型 1：基于自选词汇命名 2：基于商标命名" align="center" prop="accountNameType" />
        <el-table-column label="名称命中关键词-补充材料" align="center" prop="accountSupplemental" />
        <el-table-column label="认证类型为个人类型时可以选择要认证的身份" align="center" prop="authIdentification" />
        <el-table-column label="身份证明材料" align="center" prop="authIdentMaterial" />
        <el-table-column label="第三方联系电话" align="center" prop="thirdPartyPhone" />
        <el-table-column label="appid" align="center" prop="serviceAppid" />
        <el-table-column label="认证联系人姓名" align="center" prop="name" />
        <el-table-column label="认证联系人邮箱" align="center" prop="email" />
        <el-table-column label="认证联系人手机号" align="center" prop="mobile" />
        <el-table-column label="发票类型 1: 不开发票 2: 电子发票 4: 增值税专票" align="center" prop="invoiceType" />
        <el-table-column label="纳税识别号" align="center" prop="taxpayerId" />
        <el-table-column label="企业电话" align="center" prop="enterprisePhone" />
        <el-table-column label="企业注册地址
" align="center" prop="enterpriseAddress" />
        <el-table-column label="企业开户银行" align="center" prop="bankName" />
        <el-table-column label="企业银行账号" align="center" prop="bankAccount" />
        <el-table-column label="发票备注" align="center" prop="desc" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['platform:miniProgramAuthentication:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['platform:miniProgramAuthentication:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改小程序认证信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="miniProgramAuthenticationFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="小程序基础id" prop="baseId">
          <el-input v-model="form.baseId" placeholder="请输入小程序基础id" />
        </el-form-item>
        <el-form-item label="主体资质其他证明材料" prop="qualificationOther">
            <el-input v-model="form.qualificationOther" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="小程序账号名称" prop="accountName">
          <el-input v-model="form.accountName" placeholder="请输入小程序账号名称" />
        </el-form-item>
        <el-form-item label="名称命中关键词-补充材料" prop="accountSupplemental">
            <el-input v-model="form.accountSupplemental" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="认证类型为个人类型时可以选择要认证的身份" prop="authIdentification">
          <el-input v-model="form.authIdentification" placeholder="请输入认证类型为个人类型时可以选择要认证的身份" />
        </el-form-item>
        <el-form-item label="身份证明材料" prop="authIdentMaterial">
            <el-input v-model="form.authIdentMaterial" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="第三方联系电话" prop="thirdPartyPhone">
          <el-input v-model="form.thirdPartyPhone" placeholder="请输入第三方联系电话" />
        </el-form-item>
        <el-form-item label="appid" prop="serviceAppid">
          <el-input v-model="form.serviceAppid" placeholder="请输入appid" />
        </el-form-item>
        <el-form-item label="认证联系人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入认证联系人姓名" />
        </el-form-item>
        <el-form-item label="认证联系人邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入认证联系人邮箱" />
        </el-form-item>
        <el-form-item label="认证联系人手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入认证联系人手机号" />
        </el-form-item>
        <el-form-item label="纳税识别号" prop="taxpayerId">
          <el-input v-model="form.taxpayerId" placeholder="请输入纳税识别号" />
        </el-form-item>
        <el-form-item label="企业电话" prop="enterprisePhone">
          <el-input v-model="form.enterprisePhone" placeholder="请输入企业电话" />
        </el-form-item>
        <el-form-item label="企业注册地址
" prop="enterpriseAddress">
            <el-input v-model="form.enterpriseAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="企业开户银行" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入企业开户银行" />
        </el-form-item>
        <el-form-item label="企业银行账号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入企业银行账号" />
        </el-form-item>
        <el-form-item label="发票备注" prop="desc">
          <el-input v-model="form.desc" placeholder="请输入发票备注" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MiniProgramAuthentication" lang="ts">
import { listMiniProgramAuthentication, getMiniProgramAuthentication, delMiniProgramAuthentication, addMiniProgramAuthentication, updateMiniProgramAuthentication } from '@/api/platform/miniProgramAuthentication';
import { MiniProgramAuthenticationVO, MiniProgramAuthenticationQuery, MiniProgramAuthenticationForm } from '@/api/platform/miniProgramAuthentication/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const miniProgramAuthenticationList = ref<MiniProgramAuthenticationVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const miniProgramAuthenticationFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: MiniProgramAuthenticationForm = {
  id: undefined,
  baseId: undefined,
  customerType: undefined,
  payType: undefined,
  qualificationOther: undefined,
  accountName: undefined,
  accountNameType: undefined,
  accountSupplemental: undefined,
  authIdentification: undefined,
  authIdentMaterial: undefined,
  thirdPartyPhone: undefined,
  serviceAppid: undefined,
  name: undefined,
  email: undefined,
  mobile: undefined,
  invoiceType: undefined,
  taxpayerId: undefined,
  enterprisePhone: undefined,
  enterpriseAddress: undefined,
  bankName: undefined,
  bankAccount: undefined,
  desc: undefined,
  remark: undefined
}
const data = reactive<PageData<MiniProgramAuthenticationForm, MiniProgramAuthenticationQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    baseId: undefined,
    customerType: undefined,
    payType: undefined,
    qualificationOther: undefined,
    accountName: undefined,
    accountNameType: undefined,
    accountSupplemental: undefined,
    authIdentification: undefined,
    authIdentMaterial: undefined,
    thirdPartyPhone: undefined,
    serviceAppid: undefined,
    name: undefined,
    email: undefined,
    mobile: undefined,
    invoiceType: undefined,
    taxpayerId: undefined,
    enterprisePhone: undefined,
    enterpriseAddress: undefined,
    bankName: undefined,
    bankAccount: undefined,
    desc: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询小程序认证信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMiniProgramAuthentication(queryParams.value);
  miniProgramAuthenticationList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  miniProgramAuthenticationFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: MiniProgramAuthenticationVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加小程序认证信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: MiniProgramAuthenticationVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getMiniProgramAuthentication(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改小程序认证信息";
}

/** 提交按钮 */
const submitForm = () => {
  miniProgramAuthenticationFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateMiniProgramAuthentication(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addMiniProgramAuthentication(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: MiniProgramAuthenticationVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除小程序认证信息编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delMiniProgramAuthentication(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('platform/miniProgramAuthentication/export', {
    ...queryParams.value
  }, `miniProgramAuthentication_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
